﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Application
open Microsoft.SharePoint.Client.Sharing
open Microsoft.SharePoint.Client.Utilities
open Microsoft.SharePoint.Client.WebParts
open System

// =======================================================
// Approving access requests & reading data from columns
// =======================================================

let site = Client.Context.Site
Client.Load site

let web = Client.Context.Web
Client.Load web

let list = web.Lists.GetByTitle("Access Requests")
Client.Load list

// Approve / Decline Request
Microsoft.SharePoint.Client.AccessRequests.ChangeRequestStatus(
    Client.Context,
    1,
    0,
    "programmatically modified",
    "SharePoint Group",
    7)
Client.ExecuteQuery()

// Default View
web.AllProperties.["_VTI_PENDINGREQUESTSVIEWID"]

// Columns
Client.Load list.Fields

let accessRequest = list.GetItemById(1)
Client.Load accessRequest
accessRequest.["RequestedWebId"]
accessRequest.["RequestedListId"]
accessRequest.["RequestedListItemId"]
accessRequest.["SharedItemUrl"] 
accessRequest.["Status"]

let fields =
    [|
        "ContentTypeId"
        "Title"
        "RequestId"
        "RequestDate"
        "RequestedObjectUrl"
        "RequestedObjectTitle"


        "RequestedWebId"
        "RequestedListId"
        "RequestedListItemId"
        
        "ReqByUser"
        "RequestedBy"
        "RequestedByUserId"
        "RequestedByDisplayName"
        
        "ReqForUser"
        "RequestedFor"
        "RequestedForUserId"
        "RequestedForDisplayName"

        "ApprovedBy"
        
        "Status"
        "PermissionType"
        "PermissionLevelRequested"
    |]

for col in fields do    
    let field = list.Fields.GetByInternalNameOrTitle(col)
    Client.Load field     
    let mutable msg  = ""
    if accessRequest.[col] <> null then
        if field.TypeAsString.Contains("User") then
            let uservalue = accessRequest.[col] :?> FieldUserValue
            msg  <- col + ": " + uservalue.LookupValue
        else
            msg  <- col + ": " + accessRequest.[col].ToString() + " (" + field.TypeAsString + ")"
    else
        msg  <- col + ": " + field.TypeAsString + " (null)\n" + field.SchemaXml
    printfn "%s\n" msg    

Client.Load list.ContentTypes
let ct = list.ContentTypes.[0]
Client.Load ct

let Title = list.Fields.GetByInternalNameOrTitle("Title")
Client.Load Title

let RequestedByDisplayNameDisp = list.Fields.GetByInternalNameOrTitle("RequestedByDisplayNameDisp")
Client.Load RequestedByDisplayNameDisp

let RequestedForDisplayNameDisp = list.Fields.GetByInternalNameOrTitle("RequestedForDisplayNameDisp")
Client.Load RequestedForDisplayNameDisp

let ReqForUser = list.Fields.GetByInternalNameOrTitle("ReqForUser")
Client.Load ReqForUser

let ReqByUser = list.Fields.GetByInternalNameOrTitle("ReqByUser")
Client.Load ReqByUser

let RequestedListItemId = list.Fields.GetByInternalNameOrTitle("RequestedListItemId")
Client.Load RequestedListItemId